package com.betterjr.modules.customer.service;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.betterjr.common.utils.BTAssert;
import com.betterjr.common.utils.BetterDateUtils;
import com.betterjr.common.utils.Collections3;
import com.betterjr.common.utils.QueryTermBuilder;
import com.betterjr.common.utils.UserUtils;
import com.betterjr.modules.account.entity.CustInfo;
import com.betterjr.modules.account.service.CustAccountService;
import com.betterjr.modules.customer.AbstractBaseMechCustTemDataHandlerAdapter;
import com.betterjr.modules.customer.constants.CustomerConstants;
import com.betterjr.modules.customer.dao.CustMechLawMapper;
import com.betterjr.modules.customer.entity.CustMechLaw;
import com.betterjr.modules.customer.entity.CustMechLawTmp;
import com.betterjr.modules.document.service.CustFileItemService;
import com.betterjr.modules.temhandler.data.TemDataConstant;
import com.betterjr.modules.temhandler.entity.TemData;
import com.betterjr.modules.temhandler.entity.TemDataBuilder;
import com.betterjr.modules.temhandler.service.TemDataService;

/**
 * 
 * @author liuwlBaseService<CustMechLawMapper, CustMechLaw>
 *
 */
@Service
public class CustMechLawService extends AbstractBaseMechCustTemDataHandlerAdapter<CustMechLawMapper, CustMechLaw> {
    @Resource
    private CustAccountService accountService;

    @Resource
    private CustMechLawTmpService lawTmpService;

    @Autowired
    private CustFileItemService custFileItemService;

    @Autowired
    private TemDataService temDataService;

    /**
     * 查询法人信息
     * 
     * @param anCustNo
     * @return
     */
    public CustMechLaw findCustMechLawByCustNo(Long anCustNo) {
        BTAssert.notNull(anCustNo, "客户编号不允许为空！");

        final List<CustMechLaw> laws = this.selectByProperty(CustomerConstants.CUST_NO, anCustNo);

        return Collections3.getFirst(laws);
    }

    /**
     * 查询法人信息
     * 
     * @param anCustNo
     * @return
     */
    public CustMechLaw findCustMechLaw(Long anId) {
        BTAssert.notNull(anId, "客户编号不允许为空！");

        return this.selectByPrimaryKey(anId);
    }

    /**
     * 法人信息-修改 代录/变更
     * 
     * @param anCustMechLaw
     * @return
     */
    public CustMechLaw saveCustMechLaw(CustMechLawTmp anLawTmp) {
        BTAssert.notNull(anLawTmp, "法人流水信息不允许为空！");

        // 根据 类型区别保存数据方式
        Long custNo = anLawTmp.getRefId();
        CustMechLaw tempCustMechLaw = this.findCustMechLawByCustNo(custNo);

        BTAssert.notNull(tempCustMechLaw, "没有找到法人信息!");

        tempCustMechLaw.initModifyValue(anLawTmp);
        this.updateByPrimaryKeySelective(tempCustMechLaw);

        return tempCustMechLaw;
    }

    /**
     * 法人信息-添加
     * 
     * @param anLaw
     * @return
     */
    public CustMechLaw addCustMechLaw(CustMechLaw anLaw, Long anCustNo) {
        BTAssert.notNull(anLaw, "法人信息不允许为空！");
        BTAssert.notNull(anCustNo, "客户编号不允许为空！");

        final CustInfo custInfo = accountService.selectByPrimaryKey(anCustNo);
        anLaw.initAddValue(anCustNo, custInfo.getCustName(), custInfo.getRegOperId(), custInfo.getRegOperName(),
                custInfo.getOperOrg());
        this.insert(anLaw);

        lawTmpService.addCustMechLawTmp(anLaw);

        return anLaw;
    }

    @Override
    public TemData saveAddOrUpdateTemData(TemData anTemData) {

        // 插入校验

        Map<String, Object> map = QueryTermBuilder.newInstance().put("custNo", anTemData.getCustNo()).build();
        List<TemData> temDataList = temDataService.queryTemData(anTemData.getBusinTableType(), "0,1,2,3", map, "2", 1,
                10);
        BTAssert.isTrue(Collections3.isEmpty(temDataList), "已经有变更在申请过程中");

        return temDataService.saveAddTemData(anTemData);
    }

    public CustMechLaw saveAddCustMechLawTem(CustMechLaw custMechLaw, String anFileList) {

        BTAssert.notNull(custMechLaw, "公司法人信息-流水信息 不能为空！");
        BTAssert.notNull(custMechLaw.getId(), "公司法人信息-流水信息 不能为空！");
        BTAssert.notNull(custMechLaw.getCustNo(), "公司法人信息-流水信息 不能为空！");
        custMechLaw.saveAddInitValue(UserUtils.getOperatorInfo());
        custMechLaw.setBatchNo(custFileItemService.updateCustFileItemInfo(anFileList, custMechLaw.getBatchNo()));
        custMechLaw.setCustName(accountService.selectByPrimaryKey(custMechLaw.getCustNo()).getCustName());
        TemData anTemData = convertToTemData(custMechLaw);
        saveAddOrUpdateTemData(anTemData);

        return custMechLaw;
    }

    private TemData convertToTemData(CustMechLaw custMechLaw) {

        TemData anTemData = TemDataBuilder.newInstance().businTableData(JSONObject.toJSONString(custMechLaw))
                .businTableId(custMechLaw.getId() + "").businTableOperorg(custMechLaw.getOperOrg())
                .businTableType(custMechLaw.getClass().getName()).laster(TemDataConstant.TEMDATA_LASTER_YES)
                .custNo(custMechLaw.getCustNo()).custName(custMechLaw.getCustName())
                .regDate(BetterDateUtils.getNumDate()).regOperid(UserUtils.getOperatorInfo().getId())
                .regOpername(UserUtils.getOperatorInfo().getName()).regTime(BetterDateUtils.getNumTime())
                .businStatus(TemDataConstant.TEMDATA_BUSINSTATUS_INSERTED).build();
        return anTemData;
    }

}
